package com.zcdyy.rentalofhouse.ui.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.base.BaseActivity;
import com.zcdyy.rentalofhouse.bean.Comment;
import com.zcdyy.rentalofhouse.listener.OnItemClickListener;
import com.zcdyy.rentalofhouse.ui.adapter.SellerStateCommentAdapter;
import com.zcdyy.rentalofhouse.utils.BmobUtils;
import com.zcdyy.rentalofhouse.utils.ToastUtils;
import com.zcdyy.rentalofhouse.views.EmptyView;
import com.zcdyy.rentalofhouse.views.WritePopwindows;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

public class CommentActivity extends BaseActivity {
    private TextView title;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private List<Comment> list = new ArrayList<>();
    private EmptyView emptyView;
    private SellerStateCommentAdapter commentAdapter;
    private BmobUtils bmobUtils;
    private String id;
    public TextView comment;
    private WritePopwindows writePopwindows;
    private InputMethodManager imm;
    @Override
    protected void findViewByIDS() {
        comment = findViewsById(R.id.xiepinglun);
        emptyView = new EmptyView(this);
        title = findViewsById(R.id.top_tv_title);
        recyclerView = findViewsById(R.id.recyclerView);
        swipeRefreshLayout = findViewsById( R.id.commend_mrl);
        //设置加载图标的颜色
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorRed));
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1://获取评论
                    Bundle bundle = msg.getData();
                    List<Comment> commentlist = (List<Comment>) bundle.getSerializable("list");
                    if (commentlist!=null){
                        list.clear();
                        list.addAll(commentlist);
                        if (list.size()==0){
                            emptyView.setNotify("暂无留言");
                        }else {
                            emptyView.setEmptyViewGone();
                        }
                        commentAdapter.addList(list);
                        commentAdapter.notifyDataSetChanged();
                    }
                    swipeRefreshLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            swipeRefreshLayout.setRefreshing(false);

                        }
                    }, 1000);
                    break;
                case 404:
                    ToastUtils.shortToast(CommentActivity.this,"网络错误，请刷新");
                    break;
                case 888://评论
                    addCommentData();//增加评论列表数据
                    ToastUtils.shortToast(CommentActivity.this,"评论成功");
                    break;
            }
        }
    };

    /**
     * 增加评论列表数据
     */
    private void addCommentData() {
        Comment commentInfo = new Comment();
        commentInfo.setRentalId(id);
        commentInfo.setCommentId(loginuser.getId());
        commentInfo.setCommentName(loginuser.getNikeName());
        commentInfo.setContent(writePopwindows.word);
        if (!writePopwindows.isCommnetOrReplay()){
            commentInfo.setHasReply(!writePopwindows.isCommnetOrReplay());
            commentInfo.setReplayName(writePopwindows.getReplyName());
        }
        list.add(0,commentInfo);
        recyclerView.setAdapter(commentAdapter);
        commentAdapter.addList(list);
        commentAdapter.notifyDataSetChanged();
        writePopwindows.comment1.setText("");
//        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        if (list.size() == 0) {
            emptyView.setNotify("暂无评论");
        } else {
            emptyView.setEmptyViewGone();
        }
        this.comment.setClickable(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        initData();
        bind();
    }

    private void bind() {
        commentAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (loginuser.getId().equals(list.get(position).getCommentId())){
                    ToastUtils.shortToast(CommentActivity.this,"不能自己回复自己");
                    return;
                }
                writePopwindows.setReplyName(list.get(position).getCommentName());
                writePopwindows.setCommnetOrReplay(false);//表示回复评论
                writePopwindows.initData();
                writePopwindows.showAtLocation(getCurrentFocus(), Gravity.BOTTOM,0,0);

                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                swipeRefreshLayout.setRefreshing(true);
                bmobUtils.getCommentInfo(id,1,404,handler);
            }
        });

        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                writePopwindows.setCommnetOrReplay(true);//表示评论
                writePopwindows.initData();
                writePopwindows.showAtLocation(getCurrentFocus(), Gravity.BOTTOM,0,0);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
    }

    private void initData() {
        title.setText("留言列表");
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        commentAdapter = new SellerStateCommentAdapter(this,list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(commentAdapter);
        bmobUtils = new BmobUtils(this);
        id = getIntent().getStringExtra("id");
        writePopwindows = new WritePopwindows(this,id);
        writePopwindows.setHandler(handler);
        bmobUtils.getCommentInfo(id,1,404,handler);
    }
}
